﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace AAA.Base.Util
{
    public class DateTimeHelper
    {
        public static readonly DateTime ERROR_DATE_TIME = DateTime.MinValue;
        public static readonly string[] DATE_TIME_LIST = { "yyyy/M/d tt hh:mm:ss", "yyyy/MM/dd tt hh:mm:ss", "yyyy/MM/dd HH:mm:ss.fff", "yyyy/MM/dd HH:mm:ss:fff", "yyyy/MM/dd HH:mm:ss", "yyyy/M/d HH:mm:ss", "yyyy/M/d", "yyyy/MM/dd", "yyyy/MM", "yyyyMMdd", "yyyyMMdd HHmmss" }; 
 
        public const int SECOND = 0;
        public const int MINUTE = 1;
        public const int HOUR = 2;
        public const int DAY = 3;

        public static int GetWeekNumber(DateTime date)
        {
            return GetWeekNumber(date, CultureInfo.CurrentCulture);
        }

        public static int GetWeekNumber(DateTime date, CultureInfo culture)
        {
            return culture.Calendar.GetWeekOfYear(date,
                culture.DateTimeFormat.CalendarWeekRule,
                culture.DateTimeFormat.FirstDayOfWeek);
        }

        //public static int GetWeekOfMonth(this DateTime date)
        //{
        //    return GetWeekOfMonth(date, CultureInfo.CurrentCulture);
        //}

        //public static int GetWeekOfMonth(this DateTime date, CultureInfo culture)
        //{
        //    return date.GetWeekNumber(culture)
        //         - new DateTime(date.Year, date.Month, 1).GetWeekNumber(culture);
        //}

        public static string QuoterFromMonth(string strMonth)
        {
            return QuoterFromMonth(strMonth, ".");
        }

        public static string QuoterFromMonth(string strMonth, string strDelimeter)
        {
            try
            {
                strMonth = strMonth.Replace('.', '/').Replace('-', '/');
                string[] strValues = strMonth.Split('/');
                int iMonth = int.Parse(strValues[1]);
                return strValues[0] + strDelimeter + (Math.Floor((iMonth - 1.0) / 3.0) + 1).ToString("0") + "Q";
            }
            catch (Exception ex)
            {
                return ex.Message + "\n" + ex.StackTrace;
            }
            return null;
        }

        public static double DateDiff(int iField, string strTime1, string strTime2)
        {
            return DateDiff(iField, DateTime.Parse(strTime1), DateTime.Parse(strTime2));
        }
        public static double DateDiff(int iField, DateTime dtTime1, DateTime dtTime2)
        {
            try
            {
                TimeSpan ts1 = new TimeSpan(dtTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(dtTime2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                int iSignValue = ts1.Ticks > ts2.Ticks ? 1 : -1;

                switch (iField)
                {
                    case SECOND:
                        return ts.TotalSeconds * iSignValue;
                    case MINUTE:
                        return ts.TotalMinutes * iSignValue;
                    case HOUR:
                        return ts.TotalHours * iSignValue;
                    case DAY:
                        return ts.TotalDays * iSignValue;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return double.NaN;
        }

        public static DateTime Parse(string strValue)
        {
            int iHourOffset = 0;

            try
            {
                return DateTime.Parse(strValue);
            }
            catch(Exception ex)
            {
                strValue = strValue.Replace("上午", "");

                if (strValue.IndexOf("下午") > -1)
                {
                    strValue = strValue.Replace("下午", "");
                    iHourOffset = 12;
                }

                try
                {
                    return DateTime.Parse(strValue);
                }
                catch
                {
                    try
                    {
                        return DateTime.ParseExact(strValue,
                                                   DATE_TIME_LIST,
                                                   CultureInfo.InvariantCulture,
                                                   DateTimeStyles.AllowWhiteSpaces).AddHours(iHourOffset);
                    }
                    catch 
                    {
                        return ERROR_DATE_TIME;
                    }
                }
            }
            return ERROR_DATE_TIME;    
        }
    }
}
